package com.hainiu.cat.web.codeStudy.cas;

/**
 * create by biji.zhao on 2021/4/1
 */
public class SimulatedCAS {
    private int value;

    public synchronized int getValue() {
        return value;
    }

    /**
     * // 自旋比较
     * @param expectedValue 期望值
     * @param newValue      新值
     * @return              老值
     */
    public synchronized int compareAndSwap(int expectedValue, int newValue) {
        int oldValue = value;
        if (value == expectedValue)
            value = newValue;
        return oldValue;
    }

}
